<?php
/**
 * 用户模块 控制器
 *
 * @author    wangmx <wangmx@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Http\Controllers\Api;

use App\Services\UserService\UserService;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Response;
use App\Services\UserService\bankList;

class UserController extends BaseController
{
    protected $user;

    public function __construct(UserService $usersService)
    {
        $this->user = $usersService;
    }

    /**
     * 国家
     */
    public function country()
    {
        $result = $this->user->country();
        return Response::json($result);
    }

    /**
     * 发送验证码
     * phone,send_type,login_type
     * 普通登录 传phone send_type = 0 login_type = 0
     * 第三方登录 传phone send_type = 1 login_type = 1 2 3
     */
    public function sendIdentifying(Request $request)
    {
        $result = $this->user->sendTemplateSMS($request);
        return Response::json($result);
    }

    /**
     * 登录/注册/绑定
     * 通用参数:access_type(0注册 1登录 2第三方绑定)
     * phone(手机号) identifying(验证码) udid(设备ID)
     * 第三方绑定增加参数:openid(第三方唯一标示) login_type(1微信 2QQ 3微博)
     */
    public function login(Request $request)
    {
        $result = $this->user->login($request);
        return Response::json($result);
    }

    /**
     * 第三方登录信息储存
     * 参数:login_type(1微信 2QQ 3微博)
     * openid(第三方唯一标示) nickname(昵称) headimgurl(头像) udid(设备ID)
     */
    public function thirdPartyLogin(Request $request)
    {
        $result = $this->user->thirdPartyLogin($request);
        return Response::json($result);
    }

    /**
     * 用户信息
     * 参数:user_id
     */
    public function userinfo(Request $request)
    {
        $result = $this->user->userInfo($request);
        return Response::json($result);
    }

    /**
     * 平台内绑定第三方
     * 参数:user_id bind_type(绑定类型1微信 2QQ 3微博) openid nickname headimgurl
     */
    public function bindThirdParty(Request $request)
    {
        $result = $this->user->bindThirdParty($request);
        return Response::json($result);
    }

    /**
     * 解绑第三方
     * 参数:user_id bind_type(绑定类型1微信 2QQ 3微博)
     */
    public function cancelBindThirdParty(Request $request)
    {
        $result = $this->user->cancelBindThirdParty($request);
        return Response::json($result);
    }

    /**
     * 修改昵称
     * 参数user_id nickname(昵称)
     */
    public function modifyNickname(Request $request)
    {
        $result = $this->user->changeName($request);
        return Response::json($result);
    }

    /**
     * 修改头像
     * 参数:user_id headerImg(图片文件)
     */
    public function modifyHeaderimg(Request $request)
    {
        $result = $this->user->changeHeaderimg($request);
        return Response::json($result);
    }

    /**
     * 修改性别
     * 参数:user_id gender(性别 0保密 1男 2女)
     */
    public function modifyGender(Request $request)
    {
        $result = $this->user->changeGender($request);
        return Response::json($result);
    }

    /**
     * 修改生日
     * 参数:user_id birthday(生日)
     */
    public function modifyBirthday(Request $request)
    {
        $result = $this->user->changeBirthday($request);
        return Response::json($result);
    }

    /**
     * 修改区域
     * 参数:user_id  area(区域)
     */
    public function modifyArea(Request $request)
    {
        $result = $this->user->changeArea($request);
        return Response::json($result);
    }

    /**
     * 关注
     * 参数:user_id(当前用户ID) follow_user_id(关注人ID)
     */
    public function follow(Request $request)
    {
        $result = $this->user->follow($request);
        return Response::json($result);
    }

    /**
     * 取消关注
     * 参数:user_id(当前用户ID) follow_user_id(关注人ID)
     */
    public function cancelFollow(Request $request)
    {
        $result = $this->user->canleFollow($request);
        return Response::json($result);
    }

    /**
     * 我的关注
     * 参数:user_id
     */
    public function myFollow(Request $request)
    {
        $result = $this->user->followList($request);
        return Response::json($result);
    }

    /**
     * 是否关注
     */
    public function is_follow($user_id, $follow_user_id)
    {
        $is_follow = DB::table('follows')
            ->where(['user_id' => $user_id, 'follow_user_id' => $follow_user_id, 'state' => 1])
            ->first();
        if ($is_follow == null) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 更新封面
     */
    public function modifyCoverMap(Request $request)
    {
        $result = $this->user->changeCoverMap($request);
        return Response::json($result);
    }

    /**
     * 获取用户认证信息设计师
     */
    public function getAuthenticationDesigner(Request $request)
    {
        $result = $this->user->getAuthenticationDesigner($request);
        return Response::json($result);
    }

    /**
     * 获取用户认证信息品牌
     */
    public function getAuthenticationBrand(Request $request)
    {
        $result = $this->user->getAuthenticationBrand($request);
        return Response::json($result);
    }

    /**
     * 个人中心
     */
    public function personCenter(Request $request)
    {
        $result = $this->user->personCenter($request);
        return Response::json($result);
    }

    /**
     * 验证码 测试
     */
    public function sendCaptcha(Request $request)
    {
        $result = $this->user->sendCaptcha($request);
        return Response::json($result);
    }

    /**
     * 退出登录
     */
    public function logout(Request $request)
    {
//        dd(bankList::imgColor(public_path('/images/bank-logo/农业银行.png')));
        $result = $this->user->logout($request);
        return Response::json($result);
    }

    public function test(Request $request){
//        dd(bankList::getBankNameForCard('622908328593818718'));
//        $lists = DB::table('follows')
//            ->where('follow_user_id',10800)
//            ->where('state',1)->select('user_id')
//            ->pluck('user_id')
//            ->toArray();
//        $users = DB::table('users')->whereNotIn('user_id',$lists)->whereNotIn('user_id',[10800])->get();
//
//        foreach($users as $list){
//            $user_id  = $list->user_id;
//            DB::table('follows')
//                ->insert(['user_id' => $user_id,
//                    'follow_user_id' => 10800,
//                    'state' => 1,
//                    'created_at' => time(),
//                    'updated_at' => time(),
//                ]);
//        }
//        return '成功';
//        $phone = $request->get('phone');
//        $info = DB::table('users')->where('phone',$phone)->first();
//        if($info == null){
//            return '空';
//        }
//        $wechat = $info->wechat;
//        $qq = $info->qq;
//        $weibo = $info->weibo;
//        DB::table('wechats')->where('id',$wechat)->delete();
//        DB::table('qqs')->where('id',$qq)->delete();
//        DB::table('weibos')->where('id',$weibo)->delete();
//        DB::table('users')->where('phone',$phone)->delete();
//        return 'success';
//        $a  =  $this->user->checkMobile($request);
//        return $a;
//        $a1 = DB::table('users')->whereBetween('created_at', [1486656000, 1487001600])->count();
//        $count = DB::table('users')->count();
//        $a2 = $count-$a1;
//        return array('A1'=>$a1,'A2'=>$a2);
    }

}
